package com.chapter.sample.juc.jmm;

import ch.qos.logback.core.util.TimeUtil;

import java.util.concurrent.TimeUnit;

/**
 * 可见性保障 volatile
 */
public class VolatileSeeDemo {
    //程序不能停止
    static boolean flag = true;
    //程序停止
    //static volatile boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "----- come in");
            while (flag) {
                Integer i = new Integer('a');
            }
            System.out.println("thread over");
        }, "t1").start();

        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            flag = false;
            System.out.println(Thread.currentThread().getName() + "----- come in 修改标志");
        }, "t2").start();
    }
}
